/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


// Qt
#include <QSettings>

// local
#include "gconnectionwidget.h"



// ----------------- GConnectionWidget::GConnectionWidget() --------------------
GConnectionWidget::GConnectionWidget( QWidget * parent, Qt::WindowFlags f )
	: QWidget( parent, f )
{
		// ui
	setupUi( this );

		// grab recent hosts from settings and insert then into comboBox 
	QSettings settings;
	QStringList hosts = settings.value( "hosts" ).toStringList();
	if( comboHosts )
	{
		comboHosts->addItems( hosts );
	}

		// widget is in unconnected state at the beggining 
	_connected = false;
}
// ---------------------------------------------------------------------------


// -------------- GConnectionWidget::~GConnectionWidget() --------------------
GConnectionWidget::~GConnectionWidget()
{
		// save recent hosts in settings
	if( comboHosts )
	{
		QSettings settings;
			// get recent hosts list 
		QStringList hosts;
		for( int i = 0; i < comboHosts->count(); i++ )
		{
			hosts << comboHosts->itemText( i );
		}
			// save list 
		settings.setValue( "hosts", QVariant( hosts ) );
	}
}
// ---------------------------------------------------------------------------


// ----------------- GConnectionWidget::setConnected() -----------------------
void GConnectionWidget::setConnected()
{
	if( toolConnect )
	{
			// set connected icon
		toolConnect->setIcon( QIcon( tr( ":/res/connect_established.png") ) );
			// set connected tooltip
		toolConnect->setToolTip( tr( "Connected. Click to disconnect." ) );
			// check button - indicated that connections is established
		toolConnect->setChecked( true );
		_connected = true;
	}
}
// ---------------------------------------------------------------------------


// ------------------ GConnectionWidget::setDisconnected() -------------------
void GConnectionWidget::setDisconnected()
{
	if( toolConnect )
	{
			// set disconnected icon 
		toolConnect->setIcon( QIcon( tr( ":/res/connect_no.png") ) );
			// set disconnected tooltip
		toolConnect->setToolTip( tr( "Disconnected. Click to connect." ) );
			// uncheck 
		toolConnect->setChecked( false );
		_connected = false;
	}
}
// ---------------------------------------------------------------------------


// --------------- GConnectionWidget::on_toolConnect_clicked() ---------------
void GConnectionWidget::on_toolConnect_clicked()
{
	if( !_connected )
	{
		if( comboHosts )
		{
			emit connectToHost( comboHosts->currentText() );
		}
	}
	else
	{
		emit disconnectFromHost();
	}
}
// ---------------------------------------------------------------------------


// ----------- GConnectionWidget::on_comboHosts_activated()-------------------
void GConnectionWidget::on_comboHosts_activated( const QString & host )
{
	emit connectToHost( host );
}
// ---------------------------------------------------------------------------




// eof
